package administrator;

import home.HomePage;
import includes.MD5;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.Account;
import controllers.Logs;
import controllers.Registered;
import controllers.StudentInformation;

public class AddAccount extends WebPage {
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	private Model batchModel = new Model();
	private Model noModel = new Model();
	private Model passwdModel = new Model();
	private Model officerModel = new Model();
	private Model passwordModel = new Model();
	private Model semesterModel = new Model();
	private Model fromSchoolYearModel = new Model();
	private Model toSchoolYearModel = new Model();
	
	private DropDownChoice officer, semester;
	private List position = Arrays.asList(new String[] { "president", "secretary", "treasurer" });
	private List choice = Arrays.asList(new String[] { "1", "2"});
	
	private Model firstNameModel = new Model();
	private Model middleNameModel = new Model();
	private Model lastNameModel = new Model();
	private Model yearLevelModel = new Model();
	private List number = Arrays.asList(new String[] { "1", "2", "3", "4" });
	private DropDownChoice yearlevel;
	
	MD5 md5 = new MD5();
	
	public AddAccount() throws IOException, SQLException {
		
		TextField firstName = new TextField("studentFirstName", firstNameModel);
		firstName.setRequired(true);
		
		TextField middleName = new TextField("studentMiddleName", middleNameModel);
		middleName.setRequired(true);
		
		TextField lastName = new TextField("studentLastName", lastNameModel);
		lastName.setRequired(true);
		
		yearlevel =new DropDownChoice("studentYearLevel", yearLevelModel, number);
		
		TextField batch = new TextField("batch", batchModel);
		batch.setRequired(true);
		
		TextField no = new TextField("no", noModel);
		batch.setRequired(true);
		
		TextField pass = new PasswordTextField("password", passwdModel);
		pass.setRequired(true);
		
		TextField fromYear = new TextField("fromYear", fromSchoolYearModel);
		fromYear.setRequired(true);
		
		TextField toYear = new TextField("toYear", toSchoolYearModel);
		toYear.setRequired(true);
		
		officer =new DropDownChoice("role", officerModel, position);
		
		semester =new DropDownChoice("semester", semesterModel, choice);
		
		Form form = new Form("addAccountForm");
		
		form.add(new Button("add"){
			
			private static final long serialVersionUID = 1L;

			
			public void onSubmit(){
				
				try {
					String batch = (String) batchModel.getObject();
					String no = (String) noModel.getObject();
					String studentId = batch + "-" + no;
					String password = (String) passwdModel.getObject();
					String newPassword = md5.md5(password);
					String officer = (String) officerModel.getObject();
					String semester = (String) semesterModel.getObject();
					String fromYear = (String) fromSchoolYearModel.getObject();
					String toYear = (String) toSchoolYearModel.getObject();
					String yearLevel = (String) yearLevelModel.getObject();
					String firstName = (String) firstNameModel.getObject();
					String middleName = (String) middleNameModel.getObject();
					String lastName = (String) lastNameModel.getObject();
					
					String uploader = ((Sessions)getSession()).getDesc();
					
					Date today = new Date();
					Timestamp timestamp = new Timestamp(today.getTime());
					String ts = timestamp.toString();
					
				
					Account acc = (Account)sqlMap.queryForObject("Account.getAccount", studentId);
					Account b = (Account)sqlMap.queryForObject("Account.getUserId", officer);
					int uid = b.getUserId(officer);
					
					Account c = new Account();
					System.out.println(acc);
					System.out.println(Integer.toString(uid));
					
					if (acc!=null && Integer.toString(uid)!= "")
					{
						error("Record already exists");
					}
					else 
					{
					c.setStudent_id(studentId);
					c.setPassword(newPassword);
					c.setUserid(uid);
					String schoolYear = fromYear + "-" + toYear;
					c.setSchoolyear(schoolYear);
					c.setSemester(semester);
					
					sqlMap.insert("Account.addAccount", c);
					
					Registered reg = new Registered();
					reg.setSchoolyear(schoolYear);
					reg.setSemester(semester);
					reg.setStudent_id(studentId);
					reg.setStudent_yearlevel(yearLevel);
					reg.setStudent_name(firstName + " " + middleName + " " + lastName);
					reg.setUploader(uploader);
					reg.setDate_created(ts);
					reg.setUploader(uploader);
					sqlMap.insert("Registered.insert", reg);
					
					StudentInformation si = new StudentInformation();
					si.setDate_created(ts);
					si.setPassword(newPassword);
					si.setStudent_firstName(firstName);
					si.setStudent_middleName(middleName);
					si.setStudent_lastName(lastName);
					si.setStudent_yearlvl(yearLevel);
					si.setStudent_id(studentId);
					sqlMap.insert("StudentInformation.insertInformation", si);
					
					
					Logs log = new Logs();
					log.setLog_activity("Add Officer Account:" + officer + " " + studentId);
					log.setLog_officer(uploader);
					log.setLog_time(ts);
					
					sqlMap.insert("Logs.insert", log);
				
					AddAccount page = new AddAccount();
					setResponsePage(page);
					error("added");
					}
			}
				catch (SQLException e){
					 error("Fill up necessary fields");
					 e.printStackTrace();
				  } catch (IOException e) {
					e.printStackTrace();
				}
			}});
		
		add(new BookmarkablePageLink("home", AdministratorHome.class));
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		add(new BookmarkablePageLink("addAccount", AddAccount.class));
		add(new FeedbackPanel("error"));
		
		form.add(batch);
		form.add(no);
		form.add(pass);
		form.add(fromYear);
		form.add(toYear);
		form.add(officer);
		form.add(semester);
		form.add(firstName);
		form.add(middleName);
		form.add(lastName);
		form.add(yearlevel);
		add(form);
		
		
}

	
}
